package tt.dz.egobus.servicemanager.entities;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank;
import tt.dz.egobus.base.entity.BaseEntity;
import tt.dz.egobus.vo.KV;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Anson
 * @date 17-3-16
 * @name 车辆注册不同的服务
 * @since 1.0.1
 */
@Entity
@Table(name = "bus_register_service", schema = "servicemanager")
public class BusRegisterService extends BaseEntity {

    private static final long serialVersionUID = 4882614517176489059L;

    public static final KV<String, String> BUS_ALIAS = new KV<>("bus", "bus");

    public static final KV<String, String> BUS_SERVICE_ALIAS = new KV<>("busService", "busService");


    static {
        List<KV<String, String>> aliaseList = new ArrayList<KV<String, String>>(1);
        aliaseList.add(BUS_ALIAS);
        aliaseList.add(BUS_SERVICE_ALIAS);
        Bus.ALIAS_LIST.put(BusRegisterService.class, aliaseList);
    }

    /**
     * 车辆
     */
    @ManyToOne(targetEntity = Bus.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "bus_id", insertable = false, updatable = false)
    @Fetch(FetchMode.JOIN)
    private Bus bus;

    /**
     * 车辆id
     */
    @Column(name = "bus_id")
    @NotBlank(message = "车辆不能为空")
    private String busId;

    /**
     * 车辆服务类型
     */
    @ManyToOne(targetEntity = BusService.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "bus_service_id", insertable = false, updatable = false)
    @Fetch(FetchMode.JOIN)
    private BusService busService;

    /**
     * 车辆服务类型Id
     */
    @Column(name = "bus_service_id")
    @NotBlank(message = "车辆服务类型不能为空")
    private String busServiceId;

    /**
     * 是否有效
     */
    @Column(name = "is_validate")
    private boolean isValidate = false;


    public BusRegisterService() {
    }

    public Bus getBus() {
        return bus;
    }

    public void setBus(Bus bus) {
        this.bus = bus;
    }

    public String getBusId() {
        return busId;
    }

    public void setBusId(String busId) {
        this.busId = busId;
    }

    public BusService getBusService() {
        return busService;
    }

    public void setBusService(BusService busService) {
        this.busService = busService;
    }

    public String getBusServiceId() {
        return busServiceId;
    }

    public void setBusServiceId(String busServiceId) {
        this.busServiceId = busServiceId;
    }

    public boolean isValidate() {
        return isValidate;
    }

    public void setValidate(boolean validate) {
        isValidate = validate;
    }

    public boolean getIsValidate() {
        return isValidate;
    }

    public void setIsValidate(boolean validate) {
        isValidate = validate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BusRegisterService)) return false;

        BusRegisterService that = (BusRegisterService) o;

        if (isValidate() != that.isValidate()) return false;
        if (getBus() != null ? !getBus().equals(that.getBus()) : that.getBus() != null) return false;
        if (getBusId() != null ? !getBusId().equals(that.getBusId()) : that.getBusId() != null) return false;
        return getBusServiceId() != null ? getBusServiceId().equals(that.getBusServiceId()) : that.getBusServiceId() == null;

    }

    @Override
    public int hashCode() {
        int result = getBus() != null ? getBus().hashCode() : 0;
        result = 31 * result + (getBusId() != null ? getBusId().hashCode() : 0);
        result = 31 * result + (getBusServiceId() != null ? getBusServiceId().hashCode() : 0);
        result = 31 * result + (isValidate() ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "BusRegisterService{" +
                "bus=" + bus +
                ", busId='" + busId + '\'' +
                ", busService=" + busService +
                ", busServiceId='" + busServiceId + '\'' +
                ", isValidate=" + isValidate +
                '}';
    }
}
